import sys,os,cgi,time
sys.path.insert(0, os.path.split(sys.path[0])[0])

query = os.getenv("QUERY_STRING",'') 
kwargs = cgi.parse_qs(query)
run_profiler = int(kwargs.get('profile',['0'])[0])

def main():
	import tag

	def iso(d):
		return '-'.join([d[:4],d[4:6],d[6:8]])

	query = os.getenv("QUERY_STRING",'') 
	kwargs = cgi.parse_qs(query)
	date = kwargs.get('date',[time.strftime("%Y%m%d")])[0]

	if False:
		print "HTTP/1.1 200 ok"
		print "Content-Type: text/html"     # HTML is following
		print
		print """<html><head>
		<STYLE>
		<!--
		a {text-decoration:none}
		//-->
		</STYLE>
		</head>"""
	else:
		import head
		head.header()

	print "<body>"

	# header
	print """<h1>tags</h1>"""

	print "<table>"
	print "<tr><th>tag</th><th>count</th><th>hint</th></tr>"
	for t,jlist in sorted(tag.jobs_by_tag(verify=True).items()):
		tag_a = '<a href="status.py?date=%s&tags=%s"><pre>%s</pre></a>' % (date,t,t)
		print "<tr><td>%s</td><td><pre>%s</pre></td><td><pre>%s</pre></td></tr>" % (tag_a,len(jlist),tag.get_hint(t))
	print "</table>"

	print ".</body></html>"

if run_profiler:
	import profile
	profile.run("main()","rivulet-cgi-tags.profile.stats")
else:
	main()
